package com.ymatou.iapi.cardservice.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ymatou.iapi.cardservice.parameter.BatchGetCardInfoResponse;
import com.ymatou.iapi.cardservice.parameter.CardInfo;
import com.ymatou.iapi.cardservice.parameter.StatusCodeEnum;
import com.ymatou.iapi.cardservice.parameter.UserCardModel;
import com.ymatou.iapi.cardservice.service.BatchGetCardInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CardCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.cardservice.CardServiceMapper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.compare.AssertHelper;

public class Ts_BatchGetCardInfo {

	private static BatchGetCardInfoCall batchGetCardInfoCall = new BatchGetCardInfoCall();
	private static CardServiceMapper cardServiceMapper = SqlMapperFactory.getCardServiceMapper();
	private static String cardId = CardInfo.CardId1;
	private static String cardId_pswd = CardInfo.CardId1_Pswd;
	
	private static String cardId2 = CardInfo.CardId2;
	private static String cardId_pswd2 = CardInfo.CardId2_Pswd;
	
	private static int userId = CardInfo.userId;
	private static String userName = CardInfo.userName;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量获取卡信息");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchGetCardInfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchGetCardInfo_001() throws Exception {
		Logger.start(true, "批量获取卡信息  未绑卡");
		try {
			cardServiceMapper.updateCardAsOrigByCardId(cardId);
			cardServiceMapper.updateCardAsOrigByCardId(cardId2);
			JSONObject ret = batchGetCardInfoCall.call(Arrays.asList(cardId,cardId2));

			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			checkGetUserCardList(ret.getJSONObject("data"),Arrays.asList(cardId,cardId2));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchGetCardInfo_002() throws Exception {
		Logger.start(true, "批量获取卡信息 绑定多张卡");
		try {
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			CardCallService.bindingCard(cardId2, cardId_pswd2, userId, userName);
			
			JSONObject ret = batchGetCardInfoCall.call(Arrays.asList(cardId,cardId2));

			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkGetUserCardList(ret.getJSONObject("data"),Arrays.asList(cardId,cardId2));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchGetCardInfo_003() throws Exception {
		Logger.start(true, "批量获取卡信息 绑定多张卡,一张卡余额为0");
		try {
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			CardCallService.bindingCard(cardId2, cardId_pswd2, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, 0);

			JSONObject ret = batchGetCardInfoCall.call(Arrays.asList(cardId,cardId2));

			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkGetUserCardList(ret.getJSONObject("data"),Arrays.asList(cardId,cardId2));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchGetCardInfo_004() throws Exception {
		Logger.start(true, "批量获取卡信息 绑定多张卡,一张卡过期卡");
		try {
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			CardCallService.bindingCard(cardId2, cardId_pswd2, userId, userName);
			cardServiceMapper.updateCardEndTimeByCardId(cardId, YMTDateUtil.getBeforeOrNextDate(-1));

			JSONObject ret = batchGetCardInfoCall.call(Arrays.asList(cardId,cardId2));

			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkGetUserCardList(ret.getJSONObject("data"),Arrays.asList(cardId,cardId2));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchGetCardInfo_005() throws Exception {
		Logger.start(true, "批量获取卡信息 卡不存在");
		try {
			JSONObject ret = batchGetCardInfoCall.call(Arrays.asList("cadr"));

			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			Logger.verifyEquals(0, ret.getJSONObject("data").getJSONArray("userCardList").length(), "卡信息应没有");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchGetCardInfo_006() throws Exception {
		Logger.start(true, "批量获取卡信息 appid不存在");
		try {
			JSONObject ret = batchGetCardInfoCall.callAndGetReturnData(MapUtil.hashMap("cardIdList", new JSONArray(Arrays.asList(cardId)), "appId", ""));

			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchGetCardInfo_007() throws Exception {
		Logger.start(true, "批量获取卡信息 cardid不存在");
		try {
			JSONObject ret = batchGetCardInfoCall.call(Arrays.asList(""));

			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			Logger.verifyEquals(0, ret.getJSONObject("data").getJSONArray("userCardList").length(), "卡信息应没有");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	
	
	
	
	
	public static void checkGetUserCardList(JSONObject ret, List<String> cardIdList){
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		
		BatchGetCardInfoResponse tgtRetBean = gson.fromJson(ret.toString(), BatchGetCardInfoResponse.class);
		BatchGetCardInfoResponse actRetBean = new BatchGetCardInfoResponse();
		List<UserCardModel> cardList = new ArrayList<>();
		cardIdList.forEach(cardId->cardList.add(CardCallService.getUserCardModelFromDB(cardId)));
		actRetBean.setUserCardList(cardList);
		AssertHelper.assertResultEqual(tgtRetBean, actRetBean, "check GetUserCardList");
		
		
	}
	
	
	
}
